package service.impl;

import mapper.NewsMapper;
import mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import pojo.News;
import pojo.PageBean;
import pojo.User;
import service.Nservice;
import util.Sql;

import java.util.List;

public class NserviceImpl implements Nservice {

    @Override
    public PageBean<News> selectByPageAndCondition(int currentPage, int pageSize, News news) {
        SqlSession sqlSession = Sql.openSeeion();
        NewsMapper mapper= sqlSession.getMapper(NewsMapper .class);
        //开始索引
        int begin=(currentPage-1)*pageSize;
        int size=pageSize;
        String title= news.getTitle();
        if(title!=null && title.length()>0){
            news.setTitle("%"+title+"%");
        }

        List<News> rows=mapper.selectByPageAndCondition(begin,size,news);
        int Count =mapper.selectCountByCondition(news);
        //封装对象
        PageBean<News> pageBean=new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(Count);
        sqlSession.close();
        return pageBean;
    }

    @Override
    public void deleteByids(int[] ids) {
        SqlSession sqlSession = Sql.openSeeion();
        NewsMapper mapper= sqlSession.getMapper(NewsMapper .class);
        mapper.deleteByids(ids);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void Newsdelete(int id) {
        SqlSession sqlSession = Sql.openSeeion();
        NewsMapper mapper= sqlSession.getMapper(NewsMapper .class);
        mapper.Newsdelete(id);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public List<News> NewsSelect() {
        SqlSession sqlSession = Sql.openSeeion();
        NewsMapper mapper= sqlSession.getMapper(NewsMapper .class);
        List<News> newsList=mapper.NewsSelect();
        sqlSession.close();
        return newsList;
    }

    @Override
    public News selectById(int id) {
        SqlSession sqlSession = Sql.openSeeion();
        NewsMapper mapper= sqlSession.getMapper(NewsMapper .class);
        News news=mapper.selectById(id);
        sqlSession.close();
        return news;
    }

    @Override
    public int Newsinsert(News news) {
        SqlSession sqlSession = Sql.openSeeion();
        NewsMapper mapper= sqlSession.getMapper(NewsMapper .class);
        int count=mapper.Newsinsert(news);
        sqlSession.commit();
        sqlSession.close();
        return count;
    }

    @Override
    public void addup(int id, int view_count) {
        SqlSession sqlSession = Sql.openSeeion();
        NewsMapper mapper= sqlSession.getMapper(NewsMapper .class);
        mapper.addup(id,view_count+1);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public List<News> NewsSelectByV() {
        SqlSession sqlSession = Sql.openSeeion();
        NewsMapper mapper= sqlSession.getMapper(NewsMapper .class);
        List<News> newsList=mapper.NewsSelectByV();
        sqlSession.close();
        return newsList;
    }

    @Override
    public List<News> NewsSelectBytime() {
        SqlSession sqlSession = Sql.openSeeion();
        NewsMapper mapper= sqlSession.getMapper(NewsMapper .class);
        List<News> newsList=mapper.NewsSelectBytime();
        sqlSession.close();
        return newsList;
    }
}
